package com.wanghan.salary;

import com.wanghan.salary.config.EmailConfig;
import com.wanghan.salary.entity.UserSalary;
import com.wanghan.salary.utils.EmailSend;
import com.wanghan.salary.utils.ExcelUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author wanghan
 */
public class SalaryApplication {
    private static EmailConfig cofnig = new EmailConfig();
    private static String YES = "Y";
    private static String JUMP = "J";
    private static String CHANGE = "C";
    private static String QUIT = "Q";

    public static void main(String[] args) throws FileNotFoundException {
        //输入路径:C:\Users\admin\Desktop\test.xlsx
        String filePath = getExcelPath();
        //导入Excel
        List<UserSalary> list  = importExcel(filePath);
        //发送结果，逐条打印
        System.out.println(list.size());
        list.stream().forEach(userSalary -> {
            System.out.println(Thread.currentThread().getName() + ";" + userSalary.toString());
        });
        list.forEach(userSalary -> {
            sendEmail(userSalary);
        });

    }

    /**
     * 发送邮件
     */
    private static void sendEmail(UserSalary userSalary) {
        String body = userSalary.getUserName() + "你好，</br>" + userSalary.getSalaryYearMonth() + "工资为：" + userSalary.getSalary() + "元";
        System.out.println("发送邮箱：" + userSalary.getEmail() + ";发送内容：" + body);
        System.out.println("确认发送输入Y，修改按C，跳过J，退出Q，其他重新确认");
        String isContinue = getSystemIn();

        if (YES.equalsIgnoreCase(isContinue)) {
            EmailSend.sendEmail(cofnig, new String[]{userSalary.getEmail()}, "薪资情况", body);
        } else if (JUMP.equalsIgnoreCase(isContinue)) {
            System.out.println("跳过：" + userSalary.getUserName());
            return;
        } else if (CHANGE.equalsIgnoreCase(isContinue)) {
            System.out.println("请输入姓名：");
            String userName = getSystemIn();
            System.out.println("请输入年月：（格式为yyyy-mm）");
            String month = getSystemIn();
            System.out.println("请输入email：");
            String email = getSystemIn();
            System.out.println("请输入salary：");
            String salary = getSystemIn();
            UserSalary user = new UserSalary(userName, email, salary, month);
            sendEmail(user);
        } else {
            //其他重新确认
            sendEmail(userSalary);
        }

    }

    /**
     * 导入Excel
     *
     * @param filePath
     * @return
     */
    private static List<UserSalary> importExcel( String filePath) {
        InputStream file = null;
        List<UserSalary> users = new ArrayList<>();
        try {
            file = new FileInputStream(filePath);
            users = ExcelUtils.importData(file, 1, UserSalary.class);
            //输出导入结果
            if (CollectionUtils.isEmpty(users)) {
                System.out.println("导入结果为空，请重新输入路径");
                filePath = getExcelPath();
                users = importExcel(filePath);
            }
        } catch (FileNotFoundException e) {
            System.out.println("Excel格式有误，导入结果为空，请重新输入路径");
            filePath = getExcelPath();
            users = importExcel(filePath);
        }
        return users;
    }

    private static String getExcelPath() {
        System.out.println("请输入Excel路径,Y为默认路径当前文件夹下salary.xlsx文件。输入q退出");
        String filePath = getSystemIn();
        if (YES.equalsIgnoreCase(filePath)) {
            File file = new File("");
            String path = file.getAbsolutePath();
            filePath = path + "\\salary.xlsx";
            System.out.println("使用默认Excel路径为: " + filePath);
        } else {
            System.out.println("Excel路径为: " + filePath);
        }

        return filePath;
    }

    private static String getSystemIn() {
        String systemIn = "";
        Scanner sc = new Scanner(System.in);
        if (sc.hasNext()) {
            systemIn = sc.nextLine();
            System.out.println("输入值为：" + systemIn);
        }
        if (StringUtils.hasLength(systemIn) && QUIT.equalsIgnoreCase(systemIn.trim())) {
            System.out.println("系统退出！");
            System.exit(1);
        }
        if (!StringUtils.hasLength(systemIn)) {
            System.out.println("输入值为空，请重新输入：");
            getSystemIn();
        }
        return systemIn;
    }
}
